package com.iflytek.speech.pvc;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.iflytek.speech.NativeHandle;
import com.iflytek.speech.libisspvc;

import java.util.logging.Logger;

/**
 * 个性化发音人下载实例
 */
public class PvcSolution implements IPVCSolution, IPvcListener {
    private final static String TAG = "PvcSolution";

    private final NativeHandle NATIVE_HANDLE = new NativeHandle();
    private IPvcListener mListener;

    @Override
    public void onPVCMsgProc_(long uMsg, long wParam, String lParam) {
        Log.d(TAG, "onPVCMsgProc_() uMsg = " + uMsg + " | wParam = " + wParam + " | lParam = " + lParam);

        Message msg = Message.obtain();

        Bundle bundle = new Bundle();
        bundle.putLong("uMsg", uMsg);
        bundle.putLong("wParam", wParam);
        bundle.putString("lParam", lParam);

        msg.setData(bundle);

        if (null != mPvcListenerHandler) {
            mPvcListenerHandler.sendMessage(msg);
        } else {
            Log.e(TAG, "onSRMsgProc_() mPvcListenerHandler is null");
        }

    }

    private final static class Inner {
        private final static IPVCSolution INSTANCE = new PvcSolution();
    }

    public static IPVCSolution getInstance() {
        return Inner.INSTANCE;
    }

    @Override
    public int init(IPvcListener listener,String pvcPath) {
        if(TextUtils.isEmpty(pvcPath)){
            Log.e(TAG,"init() error,pvcPath is null");
            return -1;
        }
        mListener = listener;
        //创建实例
        int ret = libisspvc.create(NATIVE_HANDLE, pvcPath, this);
        //设置deviceId
        if (0 == ret) {
            //创建实例成功
            String machineCode = "aiui_testid_0001";
            Log.d(TAG, "set machine code:" + machineCode);
            //设置deviceid
            ret = libisspvc.setParam(NATIVE_HANDLE, libisspvc.ISS_PVC_PARAM_DEVICEID, machineCode);
            Log.e(TAG, "init() create set deviceId ret = " + ret);
            //设置本地缓存路径
            String downloadDir = pvcPath + "download";
            ret = libisspvc.setParam(NATIVE_HANDLE, libisspvc.ISS_PVC_PARAM_SAVEDIR, downloadDir);
            initHandler();
        }
        Log.e(TAG, "init() create error ret = " + ret);
        return ret;
    }

    @Override
    public int login(String phoneNumber, String loginCode) {
        return libisspvc.login(NATIVE_HANDLE, phoneNumber, loginCode);
    }

    @Override
    public String getLoginUser() {
        return libisspvc.getParam(NATIVE_HANDLE, libisspvc.ISS_PVC_PARAM_USER);
    }

    @Override
    public int logout() {
        return libisspvc.logout(NATIVE_HANDLE);
    }

    @Override
    public int getLoginCode(String phoneNumber) {
        return libisspvc.getLoginCode(NATIVE_HANDLE, phoneNumber);
    }

    @Override
    public int getSoundList(boolean isLocal) {
        //音乐列表是异步回调
        if(isLocal){
            Log.d(TAG,"listQueryLocal()");
            return libisspvc.listQueryLocal(NATIVE_HANDLE);
        }else{
            Log.d(TAG,"listQuery()");
            return libisspvc.listQuery(NATIVE_HANDLE);
        }
    }

    @Override
    public int getDownloadCode() {
        //无此方法
        //libisspvc.getDownloadCode(NATIVE_HANDLE);
        return -1;
    }

    @Override
    public int downLoadSound(String vnc) {
        return libisspvc.resDownload(NATIVE_HANDLE, vnc);
    }

    @Override
    public int stopDownLoad() {
        return libisspvc.stopRequest(NATIVE_HANDLE);
    }

    @Override
    public int unInit() {
        return libisspvc.destroy(NATIVE_HANDLE);
    }

    /************************************ 分离识别的使用和回调线程 ***************************************/

    private HandlerThread mPvcListenerHandlerThread = null; // 识别回调线程
    private PvcListenerHandler mPvcListenerHandler = null;

    /**
     * initHandler:(初始化回调线程). <br/>
     * (方法详述) <br/>
     * void
     */
    private void initHandler() {
        Log.d(TAG, "initHandler()");
        // 识别回调线程
        if (null == mPvcListenerHandlerThread) {
            mPvcListenerHandlerThread = new HandlerThread(
                    "mPvcListenerHandlerThread");
        }
        if (!mPvcListenerHandlerThread.isAlive()) {
            mPvcListenerHandlerThread.start();
        }
        if (null == mPvcListenerHandler) {
            mPvcListenerHandler = new PvcListenerHandler(mPvcListenerHandlerThread.getLooper());
        }
    }


    /**
     * SRListenerHandler
     *
     * @author mzgeng<br />
     * description: 识别回调处理<br/>
     * create: 2018-1-15 下午5:17:09<br/>
     * <p>
     * change by Windows User, 2018-1-15 下午5:17:09, reason: TODO
     */
    private class PvcListenerHandler extends Handler {

        PvcListenerHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null != msg) {
                Bundle bundle = msg.getData();
                if (null != bundle) {
                   int uMsg = (int) bundle.getLong("uMsg");
                   long wParam = bundle.getLong("wParam");
                    String lParam = bundle.getString("lParam");

                    if(null != mListener){
                        mListener.onPVCMsgProc_(uMsg,wParam,lParam);
                    }
                }
            }
        }
    }

    /********************************* END **********************************/
}
